import { message } from 'antd'
import actionType from '../action/actionType'
import { FreeDrawing } from '../../js/draw'
const { Eraser, Clear } = FreeDrawing

/**
 * 关于自由绘制的状态
 * 	只有两种状态：
 * 		历史状态
 * 		未来状态
 * 结束一个操作后，将操作放入历史中
 * 撤销和恢复即为互相转换
 */

const initState = {
	description: '',
	drawingDatas: [],
	current: null,
	pencilWidth: 5,
	eraserWidth: 10,
	history: [],
	next: [],
}

export default function freeDrawingReducer(state = initState, action) {
	switch (action.type) {
		case actionType.CREATE_A_NEW_DRAWING: {
			const { color, point } = action.payload
			const current = new FreeDrawing(color, state.pencilWidth)
			current.startDrawing(point)
			return {
				...state,
				current,
				description: '绘制',
			}
		}
		case actionType.CREATE_A_NEW_ERASING: {
			const current = new Eraser(state.eraserWidth)
			return {
				...state,
				current,
				description: '擦除',
			}
		}
		case actionType.SEND_DRAWING_DATA_TO_STORE: {
			return {
				...state,
				drawingDatas: state.drawingDatas.concat(action.payload),
				history: state.history.concat(state),
				next: [],
			}
		}
		case actionType.CLEAR_PAINTING: {
			const current = new Clear()
			current.draw()
			const newState = {
				...state,
				current,
				description: '清屏',
			}
			return {
				...state,
				history: state.history.concat(newState),
				next: [],
			}
		}
		case actionType.UNDO_FREE_DRAWING: {
			if (!state.history.length >= Math.abs(action.payload)) {
				message.error('没有退路可言')
				return { ...state }
			}
			const history = [...state.history]
			const undoList = history.splice(action.payload)
			const next = [...undoList, ...state.next]
			reFreeDraw(history)
			undoList.forEach((state) => message.info(`撤销操作：${state.description}`))
			return {
				...state,
				current: null,
				history,
				next,
			}
		}
		case actionType.REDO_FREE_DRAWING: {
			if (!state.next.length >= Math.abs(action.payload)) {
				message.error('已经到头啦')
				return { ...state }
			}
			const next = [...state.next]
			const redoList = next.splice(0, action.payload)
			const history = [...state.history, ...redoList]
			reFreeDraw(history)
			redoList.forEach((state) => message.info(`恢复操作：${state.description}`))
			return {
				...state,
				current: null,
				history,
				next,
			}
		}
		case actionType.SET_PENCIL_WIDTH: {
			return {
				...state,
				pencilWidth: action.payload,
			}
		}
		case actionType.SET_ERASER_WIDTH: {
			return {
				...state,
				eraserWidth: action.payload,
			}
		}
		case actionType.CLEAR_HISTORY: {
			message.info('清除完毕')
			return {
				...state,
				history: [],
				next: [],
			}
		}
		// reset!!!
		case actionType.RESET_STORE: {
			return initState
		}
		default: {
			return state
		}
	}
}

function reFreeDraw(history) {
	new Clear().draw()
	history.forEach((state) => {
		const { current } = state
		current.startDrawing && current.startDrawing(current.startPoint)
		current.drawingPoint
			? current.drawingPoint.forEach((point) => current.draw(point, false))
			: current.draw()
	})
}
